package lzzy.com;


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import java.util.Timer;
import java.util.TimerTask;


public class Demo_A extends Application {
    static int x = 200;
    static int y = 90;
    static double distance_x = Math.random() * 4 + 1;
    static double distance_y = Math.sqrt(25 - distance_x * distance_x);
    static Ellipse e = new Ellipse();
    static String temp1 = "left";
    static String temp2 = "down";

    // 只有第一次点击有效
    static Timer t = new Timer();
    static boolean record_start = true;
    static boolean record_stop = false;

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage s) {

        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);
        Button start = new Button("开始");
        Button stop = new Button("停止");

        // 分割线
        Line l = new Line();
        l.setStartX(0);
        l.setStartY(160);
        l.setEndY(160);
        l.setEndX(400);

        // 放置小球
        e.setCenterX((Math.random() * 400) + 1);
        e.setCenterY((Math.random() * 60) + 1);
        e.setRadiusX((Math.random() * 2) + 10);
        e.setRadiusY((Math.random() * 2) + 10);
        e.setFill(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
        start.setLayoutX(100);
        start.setLayoutY(190);
        stop.setLayoutX(250);
        stop.setLayoutY(190);
        start.setOnAction(event -> {

            if (record_start) {
                t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {

                        if (x < 20) {
                            temp1 = "right";
                        }
                        if (x > 380) {
                            temp1 = "left";
                        }

                        if (y < 20) {
                            temp2 = "up";
                        }
                        if (y > 140) {
                            temp2 = "down";
                        }
                        if (temp1.equals("left")) {
                            e.setCenterX(x -= distance_x);
                        } else {
                            e.setCenterX(x += distance_x);
                        }
                        if (temp2.equals("down")) {
                            e.setCenterY(y -= distance_y);
                        } else {
                            e.setCenterY(y += distance_y);
                        }
                    }
                }, 0, 20);
            }
            // “开始"按钮被点击且事件被触发，record=false;
            record_start = false;
            record_stop = false;
        });

        stop.setOnAction(event -> {
            System.out.println("停止反弹");
            // 当第二次点击"停止"时，小球重置
            if (record_stop) {

                distance_x = Math.random() * 4 + 1;
                distance_y = Math.sqrt(25 - distance_x * distance_x);

                e.setCenterX((Math.random() * 400) + 1);
                e.setCenterY((Math.random() * 60) + 1);
                record_stop = false;
            }
            record_stop = true;
            record_start = true;
            t.cancel();
        });

        root.getChildren().addAll(start, stop, l, e);
        s.setTitle("小球反弹");
        s.setScene(scene);
        s.show();
    }

}

